package org.deadline.model.config;

import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.deadline.model.baseapp.BaseEntity;
import org.deadline.model.baseapp.Organization;
import org.deadline.model.user.Person;
import org.hibernate.annotations.Generated;
import org.hibernate.annotations.GenerationTime;

@Entity
public class LdapServer extends BaseEntity  implements Serializable {

    @NotNull
    @Column(unique = true)
    @Size(min = 3, max = 255)
    private String domainName;

    @NotNull
    @Size(min = 3, max = 255)
    private String base;

    @NotNull
    @Size(min = 3, max = 255)
    private String url;

    @NotNull
    @Size(min = 3, max = 255)
    private String userDn;

    @NotNull
    @Size(min = 3, max = 100)
    private String password;

    @NotNull
    @Size(min = 5, max = 255)
    private String userSearchFilter;

    @NotNull
    @Size(min = 5, max = 255)
    private String userSearchBase;

    @NotNull
    @Size(min = 5, max = 255)
    private String emailSearchBase;

    @NotNull
    private boolean enabled;

    @ManyToOne
    private Organization organization;

    @ManyToOne
    private Person owner;

	@Id
	@Column(name="id",unique=true,nullable=false)
	@SequenceGenerator(name="ldap_server_seq_gen",sequenceName="ldap_server_seq")
	@GeneratedValue(generator="ldap_server_seq_gen",strategy=GenerationType.TABLE)
	@Generated(GenerationTime.INSERT)
	private Long id;

	
	public String getDomainName() {
        return this.domainName;
    }

	public void setDomainName(String domainName) {
        this.domainName = domainName;
    }

	public String getBase() {
        return this.base;
    }

	public void setBase(String base) {
        this.base = base;
    }

	public String getUrl() {
        return this.url;
    }

	public void setUrl(String url) {
        this.url = url;
    }

	public String getUserDn() {
        return this.userDn;
    }

	public void setUserDn(String userDn) {
        this.userDn = userDn;
    }

	public String getPassword() {
        return this.password;
    }

	public void setPassword(String password) {
        this.password = password;
    }

	public String getUserSearchFilter() {
        return this.userSearchFilter;
    }

	public void setUserSearchFilter(String userSearchFilter) {
        this.userSearchFilter = userSearchFilter;
    }

	public String getUserSearchBase() {
        return this.userSearchBase;
    }

	public void setUserSearchBase(String userSearchBase) {
        this.userSearchBase = userSearchBase;
    }

	public String getEmailSearchBase() {
        return this.emailSearchBase;
    }

	public void setEmailSearchBase(String emailSearchBase) {
        this.emailSearchBase = emailSearchBase;
    }

	public boolean isEnabled() {
        return this.enabled;
    }

	public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

	public Organization getOrganization() {
        return this.organization;
    }

	public void setOrganization(Organization organization) {
        this.organization = organization;
    }

	public Person getOwner() {
        return this.owner;
    }

	public void setOwner(Person owner) {
        this.owner = owner;
    }

	private static final long serialVersionUID = 1L;

	public String toJson() {
        return new JSONSerializer().exclude("*.class").deepSerialize(this);
    }

	public static LdapServer fromJsonToLdapServer(String json) {
        return new JSONDeserializer<LdapServer>().use(null, LdapServer.class).deserialize(json);
    }

	public static String toJsonArray(Collection<LdapServer> collection) {
        return new JSONSerializer().exclude("*.class").deepSerialize(collection);
    }

	public static Collection<LdapServer> fromJsonArrayToLdapServers(String json) {
        return new JSONDeserializer<List<LdapServer>>().use(null, ArrayList.class).use("values", LdapServer.class).deserialize(json);
    }

	public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	public Long getId() {
	    return this.id;
	}

	public void setId(Long id) {
	    this.id = id;
	}

	}
